var api = require("../../api/api.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");
var storageManage = require("../../manage/storageManage.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: 0,
    result: [],
    showModalStatus: false,
    windowHeight:'',
    animation:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //页面高度
    wx.getSystemInfo({
      success: function (res) {
        that.data.windowHeight = res.windowHeight;
      }
    });

    api.default.api('my_wallet', {}, function (data) {
      that.setData({
        balance: data.data.balance,
        result: data.data.result
      })
    }, function () {})
  },
  inputChange: function (e) {
    console.log(e)
    this.setData({
      amount: e.detail.value
    })
  },
   // 显示遮罩层
  showPay: function () {
    this.animationModal(true)
  },
  
  // 隐藏遮罩层
  hideModal: function () {
    this.animationModal(false)
  },

  // 遮罩层 动画
  animationModal: function (state) {
    var animation = wx.createAnimation({
      duration: 50,
      timingFunction: "linear",
      delay: 0
    })
    animation.translateY(this.data.windowHeight).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: state
    })
    setTimeout(function () {
      animation.translateY(0).step();
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 300);
  },
  payTap: utils.throttle(function () {
    var that = this;
    api.default.api('recharge', {
      amount: that.data.amount * 1
    }, function (res) {
      utils.payTap(that, res.data.order_no, function (data) { //支付成功
        //模拟
        api.default.api('pay_complate', {
          order_no: res.data.order_no
        },function(){})
        let nowBalance = ((that.data.balance * 100) + (that.data.amount * 100)) / 10000;
        let newConfig = storageManage.getGlobalConfig();
        newConfig.balance = nowBalance;
        storageManage.setGlobalConfig(newConfig);
        that.setData({
          balance: nowBalance,
          showModalStatus:false
        })

        let balance = (that.data.balance * 100 - that.data.commodity_cost * 100) / 10000;
        
      }, function (e) { })
    })
  }),
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})